package com.gljx.web.controller.JournalController;

import com.gljx.web.Entity.CarRefuelEntry;
import com.gljx.web.Entity.Department;
import com.gljx.web.Entity.JournalEntry.lCarRefuelingJournal;
import com.gljx.web.Entity.Oilspec;
import com.gljx.web.Entity.User;
import com.gljx.web.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/")
public class CarRefuelingJournalController {

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private FillingWareRegEntryService fillingWareRegEntryService;

    @Autowired
    private OilspecService oilspecService;

    @Autowired
    private OtherCarAddOilEntryService otherCarAddOilEntryService;

    @Autowired
    private CarRefuelEntryService carRefuelEntryService;

    /**
     * @Author lcx
     * @Date 2017/12/18 0018 上午 9:42
     * 查询加油站进销存统计报表
     */
    @RequestMapping(value = "/chooseCarRefuelingJournal")
    public String chooseFuelStationJournal(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user=(User) session.getAttribute("user");                                   //获取已经登录用户数据
		Department department=departmentService.queryAdminByFid(user.getFcompanyid());   //获取登录用户组织ID
		Calendar cale = Calendar.getInstance();// 得到一个Calendar的实例
        cale.add(Calendar.MONTH, -1);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        Calendar cale2 = Calendar.getInstance();// 得到一个Calendar的实例
        cale2.add(Calendar.MONTH, 0);
        cale2.set(Calendar.DAY_OF_MONTH, 0);
        Date resultDate = cale.getTime(); // 结果
        Date resultDate2 = cale2.getTime(); // 结果
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String beginTime = sdf.format(resultDate);
        String endTime = sdf.format(resultDate2);
        List<Oilspec> fillingCards = oilspecService.queryOilsperByFcontrolunitid(department.getFid());
        //把数据传到页面
        request.setAttribute("fillingCards", fillingCards);

        request.setAttribute("company", department);
        request.setAttribute("beginTime", beginTime);
        request.setAttribute("endTime", endTime);

        /*request.setAttribute("beginTime", "2017-06-01");
        request.setAttribute("endTime", "2017-06-30");*/

        return "backHome/journaling/financialJournaling/CarRefuelingJournaling/CarRefuelingJournalList";
    }

    @RequestMapping(value = "/selCarRefuelingJournal")
    @ResponseBody
    public Map selCarRefuelingJournal(HttpServletRequest request, @RequestParam(defaultValue = "", value = "cfoilspecName") String cfoilspecName,
                                      @RequestParam(defaultValue = "", value = "begindate") String begindate,
                                      @RequestParam(defaultValue = "", value = "enddate") String enddate) {

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");                                   //获取已经登录用户数据
        Department department = departmentService.queryAdminByFid(user.getFcompanyid());   //获取登录用户组织ID
        //1.查询
        Map returnmap = new HashMap();
        lCarRefuelingJournal beginningBalance = new lCarRefuelingJournal();

        Map map = new HashMap();
        //油id
        map.put("fid", cfoilspecName);
        map.put("companyId", department.getFid());
        map.put("beginTime", begindate);

        Oilspec oilspec = oilspecService.queryByPrimaryKey(cfoilspecName);

        //总数
        Double num1 = fillingWareRegEntryService.selFillingWareBeginBalance(map);

        //驾校车辆加油用的油
        Double num2 = carRefuelEntryService.selCarRefuelBeginBalance(map);
        //--其他车加油登记
        Double num3 = otherCarAddOilEntryService.selOtherCarAddOil(map);
        //查询单价
        Double price1 = fillingWareRegEntryService.selFillingWarePrice(map);

        returnmap.put("beginningBalance", addData(num1, num2, num3, price1, begindate));

        map.put("endTime", enddate);
        //本月购入
        //总数
        num1 = fillingWareRegEntryService.selFillingWareBeginBalances(map);
        //驾校车辆加油用的油
        num2 = carRefuelEntryService.selCarRefuelBeginBalances(map);
        //--其他车加油登记
        num3 = otherCarAddOilEntryService.selOtherCarAddOils(map);
        //查询单价
        price1 = fillingWareRegEntryService.selFillingWarePrices(map);

        returnmap.put("monthBuy", addData(num1, num2, num3, price1, begindate));

        //查询本月领用 驾校车加油
        List<lCarRefuelingJournal> carRefuelEntryList = carRefuelEntryService.selectofMonthAscarRefuelEntry(map);
        returnmap.put("ofMonthjx", carRefuelEntryList);
        //查询本月领用 其他车加油
        List<lCarRefuelingJournal> carRefuelEntryList2 = otherCarAddOilEntryService.selectofMonthAscarRefuelEntry(map);
        returnmap.put("ofMonthqt", carRefuelEntryList2);
        returnmap.put("gsName", department.getFnameL2());

        //查询期末结存

        return returnmap;
    }

    private lCarRefuelingJournal addData(Double num1, Double num2, Double num3, Double price1, String begindate) {
        if (num1 == null) {
            num1 = 0.0;
        }
        if (num2 == null) {
            num2 = 0.0;
        }
        if (num3 == null) {
            num3 = 0.0;
        }
        if (price1 == null) {
            price1 = 0.0;
        }

        lCarRefuelingJournal beginningBalance = new lCarRefuelingJournal();
        beginningBalance.setCount(String.valueOf(num1 - (num2 + num3)).substring(0, String.valueOf(num1 - (num2 + num3)).indexOf(".")));
        beginningBalance.setPrice(String.valueOf(price1));
        beginningBalance.setTotalprice(String.valueOf(((num1 - (num2 + num3)) * price1)));
        beginningBalance.setFbizdate(begindate);

        return beginningBalance;
    }


}
